package com.wzxy.nc.entity;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;

@Entity
@Table(name="sys_user")
@ExcelTarget("user")
public class SysUser {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	
	@Column(length = 16)
	private String name;
	
	@Column(length = 16)
	@Excel(name = "用户名", width=25, orderNum = "1", needMerge = true)
	private String username;
	
	@Column(length = 16)
	@Excel(name = "密码", orderNum = "2", needMerge = true)
	private String password;
	
	/** 删除标记:0.未删除(默认) 1.已删除 */
	@Column(name = "delete_flag",length = 2, columnDefinition = "INT default 0")
	private Integer deleteFlag = 0;
	
	@ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = {
            @JoinColumn(name = "role_id") })
	private Set<SysRole> roles; // 一个用户具有多个角色
	
	/** 学院 */
	@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch= FetchType.LAZY)
	@JoinColumn(name="college_id")
	private College college;
	
	public SysUser(){
		super();
	}
	
	public SysUser(String username, String password){
		super();
		this.username = username;
		this.password = password;
	}

	@Transient
	public Set<String> getRoleName(){
		Set<SysRole> roles = getRoles();
		Set<String> set = new HashSet<String>();
		Iterator<SysRole> it = roles.iterator();
		while(it.hasNext()){
			SysRole role = it.next();
			set.add(role.getRoleName());
		}
        return set;
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<SysRole> roles) {
		this.roles = roles;
	}

	public Integer getDeleteFlag() {
		return deleteFlag;
	}

	public void setDeleteFlag(Integer deleteFlag) {
		this.deleteFlag = deleteFlag;
	}

	public College getCollege() {
		return college;
	}

	public void setCollege(College college) {
		this.college = college;
	}		
	
}
